const Actions = require('./actions');
import { Control } from 'react-keeper'
import { monStorage, Storage } from 'clientConfig/util/StoreData';
import { Toast } from 'saltui';
const DB = require('app/db.js');
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        listData: []
    },
    onFetchReceive: function (pageNum, props, success, error) {
        let t = this;

        DB.Notice.get_notice_lists({
            schoolId: Storage.get('user.schoolId'),
            teacherId: Storage.get('user.teacherId'),
            pageIndex: pageNum
        })
            .then((content) => {
                let schoolId = content.check.schoolId;
                content = content.data.dataList;
                var list = [];
                var obj = {};
                if (content && content.length > 0) {
                    for (let item of content) {
                        var obj = {};
                        obj["schoolId"] = schoolId;
                        obj["badge"] = false;
                        obj["title"] = item.noticeTitle;
                        obj["subtitle"] = item.noticeTime;
                        obj["noticeId"] = item.noticeId;
                        obj["replayNum"] = item.replayNum;
                        obj["regularlyFlag"] = item.regularlyFlag;
                        obj["releasePosition"] = item.releasePosition;
                        obj["noticeRang"] = item.noticeRang;
                        obj["noticeType"] = item.noticeType;
                        obj["isRead"] = item.isRead;
                        obj["noticeStudentId"] = item.noticeStudentId;
                        obj["oldNoticeTime"] = item.oldNoticeTime;
                        obj["isreceived"] = true;
                        obj["content"] = [
                            { "subcontent": "", "unitcontent": item.noticeMsg }
                        ];
                        list.push(obj);
                        obj = {};
                    }
                }
                const hasNoMore = list.length < props.pageSize;
                t.data.listData = list;
                if (success){
                    success(hasNoMore, list);
                } else if (!content||!success) {
                    error();
                } 
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onFetchSent: function (pageNum, props, success, error) {
        let t = this;
        DB.Notice.get_sentnotice_lists({
            schoolId: Storage.get('user.schoolId'),
            teacherId:Storage.get('user.teacherId'),
            pageIndex: pageNum
        })
            .then((content) => {
                let schoolId = content.check.schoolId;
                content = content.data.dataList;
                var list = [];
                var obj = {};
                if (content && content.length > 0) {
                    for (let item of content) {
                        obj["schoolId"] = schoolId;
                        obj["badge"] = false;
                        obj["title"] = item.noticeTitle;
                        obj["subtitle"] = item.noticeTime;
                        obj["noticeId"] = item.noticeId;
                        obj["replayNum"] = item.replayNum;
                        obj["regularlyFlag"] = item.regularlyFlag;
                        obj["releasePosition"] = item.releasePosition;
                        obj["noticeRang"] = item.noticeRang;
                        obj["noticeType"] = item.noticeType;
                        obj["isreceived"] = false;
                        obj["replayFlag"] = item.replayFlag;//0不允许，1允许
                        obj["sentUserCount"] = item.sentUserCount;
                        obj["readedUserCount"] = item.readedUserCount;
                        obj["releaseName"] = item.eleaseName;
                        obj["noticeUrl"] = item.noticeUrl;
                        obj["content"] = [
                            { "subcontent": "", "unitcontent": item.noticeMsg }
                        ];
                        list.push(obj);
                        obj = {};
                    }
                }
                const hasNoMore = list.length < props.pageSize;
                t.data.listData = list;
                if (success) {
                    success(hasNoMore, list);
                } else if (!content||!success) {
                    error();
                }
            })
            .catch(function (error) {
                console.log(error)
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onDeleteReceive: function (cb) {
        let t = this;
        if (window.selectListItem && window.selectListItem instanceof Array && window.selectListItem.length > 0) {
            DB.Notice.del_receive_notice({
                noticeStudentIds: window.selectListItem.toString()
            }).then((content) => {
                if (content.code === '1') {
                    cb && cb();
                    window.selectListItem = null;
                }
            }).catch((error) => { // 失败 or 有异常被捕获
                // Toast.show({
                //     type: 'error',
                //     content: '删除失败'
                // });
                console.log(error)
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            })
        }

    },
    onDeleteSent: function (cb) {
        let t = this;
        console.log(window.selectListItem)
        if (window.selectListItem && window.selectListItem instanceof Array && window.selectListItem.length > 0) {
            DB.Notice.del_sent_notice({
                noticeIds: window.selectListItem.toString()
            }).then((content) => {
                if (content.code === '1') {
                    cb && cb();
                    window.selectListItem = null;
                } else if (content.code === '0') {
                    console.log(content.msg)
                }
            }).catch((error) => { // 失败 or 有异常被捕获
                // Toast.show({
                //     type: 'error',
                //     content: error
                // });
                console.log(error)
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            })
        }

    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
